package com.mrlee.realm;

import com.mrlee.bean.Admin;
import com.mrlee.bean.Module;
import com.mrlee.bean.Role;
import com.mrlee.mapper.AdminMapper;
import com.mrlee.mapper.ModuleMapper;
import com.mrlee.mapper.RoleMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * Created by dllo on 18/3/7.
 */
public class MyPerRealm extends AuthorizingRealm{

    @Override
    public String getName() {
        return "myPerRealm";
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof UsernamePasswordToken;
    }

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private ModuleMapper moduleMapper;

    @Resource
    private AdminMapper adminMapper;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        // 授权的方法

        // 将认证过的方法中返回的对象取出
        String username = (String) principalCollection.getPrimaryPrincipal();


        // ************假数据开始****************

        List<Role> roles = roleMapper.findRoleByUsername(username);

        Set<Module> modules = new TreeSet<Module>();

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        if (roles!=null&&roles.size()>0){
            for (Role role : roles){
                info.addRole(role.getName());
                Set<Module> moduleSet = moduleMapper.findModuleByRoleId(role.getRoleId());
                for (Module module : moduleSet){
                    modules.add(module);
                }
            }
        }
        for (Module module : modules){
            info.addStringPermission(module.getName());
        }

        // ************假数据结束****************


        // 授权循序对象, 用来保存所有的角色和授权信息

        return info;
    }



    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        // 1. 获取token中的用户名
//        AdminService adminService = new AdminServiceImpl();

        String username = (String) authenticationToken.getPrincipal();
        System.out.println(username);
        String password = new String((char[])authenticationToken.getCredentials());
        System.out.println(password);
        Admin admin = adminMapper.dologin(username,password);
//        System.out.println(admin);
        if (!admin.getAdminCode().equals(username)){
            throw new UnknownAccountException("账号或密码错误");
        }
        if (!admin.getPassword().equals(password)){
            throw new IncorrectCredentialsException("账号或密码错误");
        }


//        // ************假数据开始****************
//
//        if (!"wangwu".equals(username)){
////            return null;
//            throw new UnknownAccountException("未知账号/没有该账号");
//        }
//        // ************假数据结束****************
//
//        // 如果账号存在, 接着判断密码
//
//
//        // ************假数据开始****************
//
//        if (!"123".equals(password)){
//            throw new IncorrectCredentialsException("密码错误");
//        }
//
//        // ************假数据结束****************
        Admin ad1 = new Admin();
        ad1.setAdminCode(admin.getAdminCode());
        ad1.setName(admin.getName());
        ad1.setEmail(admin.getEmail());
        ad1.setTelephone(admin.getTelephone());
        ad1.setEnrolldate(admin.getEnrolldate());
        ad1.setAdminId(admin.getAdminId());
        ad1.setPassword(admin.getPassword());

        return new SimpleAuthenticationInfo(ad1, password, getName());
    }


}
